//
//  FKPayResultVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/19.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKPayResultVC.h"
#import "FKOrderVC.h"
#import "FKCommodityDetailVC.h"
@interface FKPayResultVC ()

@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIButton *leftBtn;
@property (nonatomic, weak) UIButton *rightBtn;

@property(nonatomic,strong)UILabel *ls_daojishiLabel;
@end

@implementation FKPayResultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.isFailure ? @"支付失败" : @"支付成功";
    
    UIImageView *iconView = [[UIImageView alloc] init];
    [self.view addSubview:iconView];
    self.iconView = iconView;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(17) textColor:fkColor333333];
    [self.view addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UIButton *leftBtn = [UIButton fk_btnWithTarget:self action:@selector(leftBtnClicked:) font:fk_adjustFont(14) normalTitle:nil normalTitleColor:fkPriceRed selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_white"] selectedBgImg:nil];
    [self.view addSubview:leftBtn];
    self.leftBtn = leftBtn;
    [leftBtn fk_viewCornerRadius:fk_adjustW(17) borderWidth:0.8 borderColor:fkMainColor];
    
    UIButton *rightBtn = [UIButton fk_btnWithTarget:self action:@selector(rightBtnClicked:) font:fk_adjustFont(14) normalTitle:nil normalTitleColor:fkColor666666 selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_white"] selectedBgImg:nil];
    [self.view addSubview:rightBtn];
    self.rightBtn = rightBtn;
    [rightBtn fk_viewCornerRadius:fk_adjustW(17) borderWidth:0.8 borderColor:fkColor666666];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(60));
        make.top.offset(fk_adjustW(40));
        make.centerX.offset(0);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconView.mas_bottom).offset(fk_adjustW(15));
        make.centerX.offset(0);
    }];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(fk_adjustW(112));
        make.height.offset(fk_adjustW(34));
        make.left.offset(fk_adjustW(32));
        make.top.equalTo(titleLabel.mas_bottom).offset(fk_adjustW(44));
    }];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.top.equalTo(leftBtn);
        make.right.offset(fk_adjustW(-32));
    }];
    
    if (self.isFailure) {
        iconView.image = [UIImage imageNamed:@"fk_pay_failure"];
        titleLabel.text = @"支付失败";
        [leftBtn setTitle:@"重新支付" forState:UIControlStateNormal];
        [rightBtn setTitle:@"返回首页" forState:UIControlStateNormal];
        
    } else {
        
       
        
        //1的话自动跳转
        if ([fkFORMAT(@"%@",self.payInfoDict[@"is_open"])  isEqualToString:@"1"]) {
            //2019-10-24号修改倒计时跳转
            UILabel *daojishiLabel=[[UILabel alloc]init];
            [self.view addSubview:daojishiLabel];
            daojishiLabel.font= fk_adjustFont(14);
            daojishiLabel.text=@"5秒后将自动跳转";
            self.ls_daojishiLabel = daojishiLabel;
            daojishiLabel.textColor = [UIColor blackColor];
            daojishiLabel.backgroundColor=[UIColor clearColor];
            [daojishiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.offset(0);
                make.top.equalTo(titleLabel.mas_bottom).offset(fk_adjustW(44));
            }];
            
            leftBtn.hidden = YES;
            rightBtn.hidden = YES;
            [self autoPushOtherVc];
        }else{
            leftBtn.hidden = NO;
            rightBtn.hidden = NO;
            iconView.image = [UIImage imageNamed:@"fk_pay_successful"];
            titleLabel.text = @"支付成功";
            [leftBtn setTitle:@"查看订单" forState:UIControlStateNormal];
            [rightBtn setTitle:@"返回首页" forState:UIControlStateNormal];
            
        }
       
    }
    
   
    
}
 -(void)autoPushOtherVc
{
    // 倒计时时间
    __block NSInteger timeOut = 5;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    // 每秒执行一次
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        // 倒计时结束，关闭
        if (timeOut <= 0) {
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
               self.ls_daojishiLabel.attributedText = [fkFORMAT(@"%@秒后将自动跳转",@"0") fk_addAttributeStyle:@"0" color:[UIColor redColor] font:fk_adjustFont(16)];
                if ([fkFORMAT(@"%@",self.payInfoDict[@"is_open"]) isEqualToString:@"1"]) {
                    FKCommodityDetailVC *VC = [FKCommodityDetailVC new];
                    VC.itemId = fkFORMAT(@"%@",self.payInfoDict[@"item_id"]);
                    fkPushNav(VC);
                }
            });
        } else {
            int seconds = timeOut % 60;
            NSString * timeStr = [NSString stringWithFormat:@"%0.2d",seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.ls_daojishiLabel.attributedText = [fkFORMAT(@"%@秒后将自动跳转",timeStr) fk_addAttributeStyle:timeStr color:[UIColor redColor] font:fk_adjustFont(16)];
            });
            timeOut--;
        }
    });
    
    dispatch_resume(_timer);
}

- (void)leftBtnClicked:(UIButton *)btn
{
//    if ([btn.titleLabel.text isEqualToString:@"重新支付"]) {
//        FKOrderVC *vc = [[FKOrderVC alloc] init];
//        vc.type = @"2";
//        fkPushNav(vc);
//    } else if ([btn.titleLabel.text isEqualToString:@"查看订单"]) {
//        FKOrderVC *vc = [[FKOrderVC alloc] init];
//        vc.type = @"2";
//        fkPushNav(vc);
//    }
    FKOrderVC *vc = [[FKOrderVC alloc] init];
//    vc.title = @"物流订单";
    vc.type = self.type ? : @"2";
    vc.needRemoveBeforeVc = true;
    fkPushNav(vc);
}

- (void)rightBtnClicked:(UIButton *)btn
{
    fkPopVC;
}


@end
